package com.sohu.auto.mobile.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.annotation.Resource;

import net.sf.ehcache.Cache;

import org.springframework.stereotype.Service;

@Service
public class BaseService {
	
	@Resource(name="userInputCache")
	protected Cache cache;
	
	protected String getBody(String strUrl, String charset) {
		StringBuffer sb = new StringBuffer();
		try {
			URL url = new URL(strUrl);
			HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpUrlConn.getInputStream(), charset));
			String str = null;
			while ((str = bufferedReader.readLine()) != null) {  
			    sb.append(str);
			}
			bufferedReader.close();
			bufferedReader = null;
			httpUrlConn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	protected String convertModel(String model, String[] args) {
		for(String s : args) {
			model = model.replaceFirst("PARAM", s);
		}
		return model;
	}
}
